﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dto;
using Model;

namespace DVT.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index(string keyWords)
        {
           // string keyword = Request.Form["keyWords"];
            string keyword = keyWords;
            ViewBag.SearchKeyword = keyword;
            IList<NewsDto> newsDto = new List<NewsDto>();
            if (!string.IsNullOrEmpty(keyword))
            {
                using (Model.NewsDBEntities entities = new Model.NewsDBEntities())
                {
                    var news = entities.News.Where(x => x.Title.Contains(keyword) ||x.NewsContent.Contains(keyword)).ToList();

                    news = news.Where(x => Common.HtmlHelper.checkStr(x.Title).Contains(keyword) || Common.HtmlHelper.checkStr(x.NewsContent).Contains(keyword)).ToList();

                    foreach (News n in news)
                    {
                        NewsDto newsdto = new NewsDto();
                        newsdto.Id = n.Id;
                        newsdto.Content = n.NewsContent;
                        newsdto.Title = n.Title;
                        newsdto.PostDate = n.PostDate;
                        newsDto.Add(newsdto);
                    }

                    
                    ViewBag.NewsCount = newsDto.Count.ToString();
                }
            }
            ViewBag.NewsList = newsDto;
            ViewBag.NewsCount = newsDto.Count.ToString();
            return View();
        }

    }
}
